package com.mk.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mk.common.request.MonitorWaterReq;
import com.mk.dao.domain.WarningConfig;
import com.mk.dao.domain.WaterLevelInfo;
import com.mk.dao.interfaces.WarningConfigDao;
import com.mk.dao.interfaces.WaterLevelInfoDao;
import com.mk.utils.MqttUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author XueJinCha
 */
@Service
@RequiredArgsConstructor
public class WaterMonitorService {

    private final MqttUtil mqttUtil;
    private final WarningConfigDao warningConfigDao;
    private final WaterLevelInfoDao waterLevelInfoDao;

    public void waterMonitor(MonitorWaterReq req) {
        WarningConfig warningConfig = warningConfigDao.getOne(new LambdaQueryWrapper<WarningConfig>()
                .eq(WarningConfig::getId, req.getWaringId()));
        if (ObjectUtils.isNull(warningConfig)) {
            mqttUtil.sendMessage("false");
            return;
        }
        if (warningConfig.getWarningValue() > req.getWaterLevel()) {
            mqttUtil.sendMessage("true");
        }
        WaterLevelInfo waterLevelInfo = new WaterLevelInfo();
        waterLevelInfo.setUserId(req.getUserId());
        waterLevelInfo.setWaterLevel(req.getWaterLevel());
        waterLevelInfoDao.save(waterLevelInfo);
    }
}
